/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef CAST_ENGINE_DLNA_SERVICE_H
#define CAST_ENGINE_DLNA_SERVICE_H

#include "cast_engine_common.h"
#include "system_ability.h"
#include "if_system_ability_manager.h"
#include "cast_engine_dlna_service_stub.h"
#include "dlna_stream_manager.h"
#include "dmr_listener.h"
#include "i_dlna_player_listener.h"
#include "ui/rs_surface_node.h"
#include "window.h"
#include "dlna_client_app_died_listener.h"
#include "cast_engine_dlna_event_controller.h"

namespace OHOS {
namespace CastEngine {
namespace CastEngineService {
class CastEngineDlnaService : public SystemAbility , public CastEngineDlnaServiceStub {
    DECLARE_SYSTEM_ABILITY(CastEngineDlnaService);
public:
    CastEngineDlnaService(int32_t saId, bool runOnCreate);
    ~CastEngineDlnaService();

    void OnStart() override;
    void OnStop() override;

    int32_t Play() override; 
    int32_t Pause() override;
    int32_t Stop() override;
    int32_t Forward(int &seconds) override;
    int32_t Backward(int &seconds) override;
    int32_t GetPosition(int &position) override;
    int32_t GetPlayerStatus(int32_t &playerStates) override;
    int32_t GetDuration(int32_t &playerStates) override;
    int32_t Seek(int position) override;

    int32_t SendDmrPlayer(
    /* [in] */ const sptr<IRemoteObject> &object) override;
    int32_t SetSurfaceId(std::string surfaceId) override;
    int32_t SetDmrSurface(sptr<IBufferProducer> producer) override;
    int32_t GetDlnaDeviceName(std::string &deviceName) override;
    int32_t SetDlnaDeviceName(const std::string &deviceName) override;
    int32_t GetDmrState(DmrStates &dmrState) override;
    int32_t GetVolume(int &volume, int &maxVolume) override;
    int32_t SetVolume(int volume) override;
    int32_t AuthDeviceFail() override;
    sptr<Surface> GetSubWindowSurface();
    int32_t OnPlayerReady(std::string clientIP);
    int32_t OnPlayerEnd();
    int32_t OnPhotoReady(std::string uri);
    int32_t OnPositionChanged(int position, int duration);
    int32_t OnStateChanged(const PlayerStates playbackState);
    int32_t OnVolumeChanged(int volume, int maxVolume);
    
private:
    void AddWatchParameter();
    void OnConnectvityChange(int32_t code);
    static void OnDlnaSyStemParamChange(const char *key, const char *value, void *context);
    int32_t DmrOpen();
    int32_t DmrClose();
    int position_;
    bool isAppRuning_ = false;
    bool isGalleryRuning_ = false;
    DmrStates dmrState_ = DmrStates::DMR_STATE_UNAVAILABLE;
    sptr<IDlnaPlayer> dlnaPlayer_ = nullptr;
    sptr<Rosen::Window> previewWindow_ = nullptr;
    sptr<CastEngineClient::DlnaClientAppProxy> dlnaClient_ = nullptr;
    sptr<CastEngineClient::DlnaClientAppDiedListener> dlnaClientAppDiedListener_ = nullptr;

};

} // namespace CastEngineService
} // namespace CastEngine
} // namespace OHOS

#endif /* CAST_ENGINE_DLNA_SERVICE_H */
